﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class RepairLookup : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            lstRepair.AllowUpdating = false;

            if (!IsPostBack)
            {
                Brands brand = Brands.LoadAllActiveBrands();
                Utilities.LoadDropDownList(ddlBrand, brand.DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID);
            }
        }

        private void ReloadAll()
        {
            View_RepairJobs jbs = View_RepairJobs.GetCurrentJobs();
            jbs.Sort = View_RepairJobs.ColumnNames.CreatedDate + " desc";
            lstRepair.Jobs = jbs;
            lstRepair.SortExpression = View_RepairJobs.ColumnNames.CreatedDate;
            lstRepair.SortDirection = BaseListControl.SORT_DESC;
            lstRepair.Refresh();
        }

        protected void ddlBrand_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RepairModels models = RepairModels.GetModelsByBrandID(int.Parse(ddlBrand.SelectedValue));
            Utilities.LoadDropDownList(ddlModel, models.DefaultView, RepairModels.ColumnNames.ModelName, RepairModels.ColumnNames.ID);
        }

        protected void ddlModel_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModel.SelectedIndex <= 0)
                return;
            int modelID = int.Parse(ddlModel.SelectedValue);

            View_RepairJobs jbs = View_RepairJobs.GetJobByModelID(modelID);
            jbs.Sort = View_RepairJobs.ColumnNames.CreatedDate + " desc";
            lstRepair.Jobs = jbs;
            lstRepair.JobProblems = View_RepairJobProblems.GetJobProblemsByRepairModelID(modelID);
            lstRepair.JobStatusHist = View_RepairStatusHistories.GetJobStatusHistByRepairModelID(modelID);
            lstRepair.SortExpression = View_RepairJobs.ColumnNames.CreatedDate;
            lstRepair.SortDirection = BaseListControl.SORT_DESC;
            lstRepair.CurrentPageIndex = 0;
            lstRepair.Refresh();
        }


        protected void btnLoadByJobID_OnClick(object sender, EventArgs e)
        {
            View_RepairJobs jbs = View_RepairJobs.GetJobByNo(txtJobID.Text);
            jbs.Sort = View_RepairJobs.ColumnNames.CreatedDate + " desc";
            lstRepair.Jobs = jbs;
            lstRepair.JobProblems = View_RepairJobProblems.GetJobProblemsByNo(txtJobID.Text);
            lstRepair.JobStatusHist = View_RepairStatusHistories.GetJobStatusHistByNo(txtJobID.Text);
            lstRepair.SortExpression = View_RepairJobs.ColumnNames.CreatedDate;
            lstRepair.SortDirection = BaseListControl.SORT_DESC;
            lstRepair.CurrentPageIndex = 0;
            lstRepair.Refresh();
        }

        protected void btnLoadByIMEI_OnClick(object sender, EventArgs e)
        {
            View_RepairJobs jbs = View_RepairJobs.GetJobsByIMEI(txtIMEI.Text);
            jbs.Sort = View_RepairJobs.ColumnNames.CreatedDate + " desc";
            lstRepair.Jobs = jbs;
            lstRepair.JobProblems = View_RepairJobProblems.GetJobProblemsByIMEI(txtIMEI.Text);
            lstRepair.JobStatusHist = View_RepairStatusHistories.GetJobStatusHistByIMEI(txtIMEI.Text);
            lstRepair.SortExpression = View_RepairJobs.ColumnNames.CreatedDate;
            lstRepair.SortDirection = BaseListControl.SORT_DESC;
            lstRepair.CurrentPageIndex = 0;
            lstRepair.Refresh();
        }
    }
}